#!/usr/bin/env tclsh

package provide widget [lindex {$Revision: 1.0 $} 1]
namespace eval A::widget {}

proc getJava {fileData} {
      set lines [split $fileData "\n"]
      set found 0
      set name ""
      set output ""
      foreach line $lines {
        switch -regexp $line {
          Button {
            set found 1
            set type "Button"
            if [regexp id\/(.*)\" $line match name] {
              append output [getJavaBlock $type $name]
              set found 0
            }
          }
          TextView {
            set found 1
            set type "TextView"
            if [regexp id\/(.*)\" $line match name] {
              append output [getJavaBlock $type $name]
              set found 0
            }
          }
          default {
          }
        }
        if $found {
          if [regexp id\/(.*)\" $line match name] {
            append output [getJavaBlock $type $name]
            set found 0
          }
        }
    }
    return $output
}

#Proceedure name: gen
#Summary: Proc gen parses a XML or Java file and generate Java
#code from XML file or XML tags from Java code
#Parameter: <workspace> IDE work space ie Eclipse work space
#Parameter: <projects> is the projects in work space
#Parameter: <fileExt> file extention determines what to generate
proc gen {workspace projects fileExt} {
  set res "/res/"
  set src "/src/"
  set basePath ""

  switch -regexp $fileExt {
       xml {
         set basePath $workspace$projects$res
       }
       java {
         set basePath $workspace$projects$src
       }
       default {
       }
  }

  foreach project $projects {
    foreach file [fileutil::findByPattern $basePath *.$fileExt] {
      set fp [open $file r]
      switch -regexp $fileExt {
         xml {
           set output [getJava [read $fp]]
         }
         java {
           set output [getXml [read $fp]]
         }
         default {
         }
      }
      close $fp
      if {[string length $output]} {
        puts "\n==========================================================================\r"
        puts "$file\n"
        puts $output
      }
    }
  }
}

proc getXml {fileData} {
      set type ""
      set name ""
      set output ""
      set lines [split $fileData "\n"]
      set found 0
      foreach line $lines {
        switch -regexp $line {
          "class.*extends Activity" {
            set found 1
            set type "activity"
            if [regexp "class (.*) extends Activity" $line match name] {
              append output [getXmlTag $type $name]
              set found 0
            }
          }
          "class.*extends Service" {
            set found 1
            set type "service"
            if [regexp "class (.*) extends Service" $line match name] {
              append output [getXmlTag $type $name]
              set found 0
            }
          }
          default {
          }
        }
        if $found {
          if [regexp id\/(.*)\" $line match name] {
            append output [getXmlTag $type $name]
            set found 0
          }
        }
      }
    return $output
}

proc getXmlTag {type name} {
  return "\r
        <!-- $name $type -->
        <$type android:name=\".$name\" android:label=\"@string\/app_name\"
            android:screenOrientation=\"portrait\">
        <\/$type>
    \n"
}

proc getJavaBlock {type name} {
  return "\r
    \/\/Instantia widget and event handling code for: $type named $name\r
    final $type $name$type = ($type) findViewById(R.id.$name);\r
    $name$type.setOnClickListener(new OnClickListener() {\r
        public void onClick(View v) {\r
           \/\/Input code here\r
        }\r
    });\n"
}
